package com.example.zhuangao6xiangm.okhttp;

import com.fhz.utilsfhz.LiveDataCallAdapterFactory;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ShopHttp {
    public ShopHttp() {
    }
    private static ShopHttp shopHttp;
    public static ShopHttp getShopHttp(){
        if (shopHttp == null){
            shopHttp = new ShopHttp();
        }
        return shopHttp;
    }
    private Retrofit retrofit;
    public Retrofit getRetrofit(){
        if (retrofit == null){
            retrofit = new Retrofit.Builder()
                    .baseUrl("http://82.156.178.182:8087/")
                    .client(new OkHttpClient.Builder()
                            .callTimeout(60, TimeUnit.SECONDS)
                            .readTimeout(60, TimeUnit.SECONDS)
                            .writeTimeout(60, TimeUnit.SECONDS)
                            .addInterceptor(new Interceptor() {
                                @NotNull
                                @Override
                                public Response intercept(@NotNull Chain chain) throws IOException {
                                    Request build = chain.request().newBuilder().addHeader("Authorization", "bearer c53PEesAjbIH59ktV4oCJgDOiE2Qmf_6ep1HlKDHr8SdCPsm0W_GTgGTzQ8w8NJpYdVBgNn5ck8fSEvprZ5FNCJEIdR7KIsynBW7PNwRkD-gaCexehaprLiPuEjEoVQu_Yx7OsFYsUU4f7vYXRo94gxUsyGF4vBOOCctNKUNFx6sV_HeaGwxWoHXdxrLbj7b5KA_7ayGBY7ujuyDP-K35dECZIRttn3wZ6X96KkQ6Zc5CiuWO_pouXuL7y9sm34Kz8gHp-tYZiYhxfehLwot96E98rk_leyO_FqTu-KUnrc").build();
                                    return chain.proceed(build);
                                }
                            })
                            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                        .build())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(new LiveDataCallAdapterFactory())
                    .build();
        }
        return retrofit;
    }
}
